package application;

import java.rmi.RemoteException;

import javax.xml.rpc.Stub;

import model.MobileContent;
import model.MobileGame;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.ui.UiApplication;
import ui.screen.*;
import ws.Service1Soap_Stub;

public class AppIntrusos extends UiApplication{	
	public static MobileGame[] games;
	public static MobileContent[] contents;
	private static boolean wsStatus = true;
	
	public static void main( String[] args ) throws RemoteException {		
		AppIntrusos theApp = new AppIntrusos();
		theApp.enterEventDispatcher();
	}
	
	public AppIntrusos() throws RemoteException {
		final SplashScreen splashScreen = new SplashScreen(10);
		pushScreen( splashScreen );
	    invokeLater(new Runnable() {
			public void run() {
				getWSData();
				
				if (wsStatus == false){

					contents = new MobileContent[2];
					contents[0] = new MobileContent();
//					contents[0].setImgURL("backPYG.jpg");
					contents[0].setImgURL("fondopremio.jpg");
					contents[0].setDescription("Fondo");
					contents[1] = new MobileContent();
//					contents[1].setImgURL("pr_comodin.png");
					contents[1].setImgURL("pr_pesos.gif");
					contents[1].setDescription("Premio");

					games = new MobileGame[3];
					games[0] = new MobileGame();
					games[0].setCC("90100");
					games[0].setGameID(5);
					games[0].setText("INTRUSOSAPP");
					games[1] = new MobileGame();
					games[1].setCC("90100");
					games[1].setGameID(13);
					games[1].setText("RIALAPP");	
					games[2] = new MobileGame();
					games[2].setCC("3002");
					games[2].setGameID(14);
					games[2].setText("RIALAPP");	
					//games[3] = new MobileGame();
					//games[3].setCC("9009");
					//games[3].setGameID(0);
					//games[3].setText("RIAL");
				}	
				
				pushScreen(new HomeScreen());
				popScreen(splashScreen);
			}
		}, 600*3,false);
		
    }
	
	public static int getIndexByGameID(int GameID){
		int ret = -1;		
		for (int i=0;i<games.length;i++){
		if (games[i].getGameID() == GameID){
			ret = i;
		}
		}		
		return ret;
	}
	
	private void getWSData(){
		try{
        	String suffix;
        	if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B))
        		suffix = ";deviceside=false;ConnectionType=mds-public";
        	else
        		suffix = ";deviceside=true";
        	Service1Soap_Stub serv = new Service1Soap_Stub();
        	serv._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, "http://wsmobileapplications.telinfor.com/wsmobileapplication.asmx"+suffix);
        	serv._setProperty(Stub.SESSION_MAINTAIN_PROPERTY, Boolean.FALSE);
        	
        	games = serv.getGamesByAppID(2);  
        	contents = serv.getContentByAppID(2);
        }catch (Exception e){ 
        	wsStatus = false;
		}
	}
}
